<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8"/>
    <script type="text/javascript" src="js/jquery-1.2.6.js"></script>
    <script type="text/javascript">
        if(getCookie("Language")){
            document.write('<script type="text/javascript" src="js/'+getCookie("Language")+'.js"><\/script>');
        }else{
            document.write('<script type="text/javascript" src="js/chinese.js"><\/script>');
        }
    </script>
    <script type="text/javascript">
        var langauaCookie =getCookie("Language");
        var languageType="chinese";
           if(langauaCookie==null)
        {
            $.ajax({
                type:"POST",
                url:"xml?method=gw.config.language&id=74",
                timeout:10000,
                dataType:"xml",
                success:function(data){
                    var language=$(data).find('item').attr('value');
                    if(language==0){
                        languageType ="chinese";
                        setCookie("Language", "chinese");
                    }else{
                        languageType ="english";
                        setCookie("Language", "english");
                    }
                    window.location.reload();
                }
            });
        }
    </script>
    <link type="text/css" rel="stylesheet" href="style/login.css"/>
    <title></title>
</head>
<body class="main_login">
        <div class="login" id="login">
            <p class="one"></p>

            <p class="two"></p>

            <p class="three"></p>

            <div id="login_top">
                <table width="452px" height="247px" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td colspan="2"> &nbsp;
                    <span style="margin-right: 8px;float:right;" id="changeLanguage">
                    <a style="float: right;"><img src="images/english.png" alt="" style="cursor:pointer" id="chooseEn"/></a>
                    <a style="float: right;"><img src="images/chinese.png" alt="" style="cursor:pointer" id="chooseCh"/></a>
                     </span>
                        </td>
                    </tr>
                    <tr>
                        <td id="td_logo" style=""><img src="ST/login_logo.png" ></td>
                        <td><span style="margin-right:120px;font-size: 30px;" id="position"><img src="ST/product.png" id="product_image"></span></td>
                    </tr>
                    <tr>
                        <td colspan="2">&nbsp;</td>
                    </tr>
                    <tr>
                        <td id="td_user"><label sid="login.username"></label></td>
                        <td align="left"><input name="username" maxlength="15" value="admin" id="name"
                                                class="login_text"/></td>
                    </tr>
                    <tr>
                        <td id="td_password"><label sid="login.password"></label></td>
                        <td align="left"><input type="password" name="password" maxlength="15"
                                                class="login_text"/></td>
                    </tr>
                    <tr>
                        <td colspan="2">&nbsp;</td>
                    </tr>
                </table>
            </div>
            <div id="login_bottom">
                <table width="100%" height="103px" border="0" cellpadding="0" cellspacing="0">
                    <tr width="100%" height="100%">
                        <td id="bottom_left" height="103px" width="5px">&#160;</td>
                        <td id="div_bottom" height="103px" width="442px">
                            <a onclick="loginWeb();"><img src="images/login_button.png" border="0" id="login_button"></a><br/><br/><br/><br/>
                        </td>
                        <td id="bottom_right" height="103px" width="5px">&#160;</td>
                    </tr>
                </table>
            </div>
            <input type="submit" class="button" style="display:none;">
        </div>


<script type="text/javascript">
var flag =1;
var sendtext = "&id=75";
var url="xml?method=gw.config.language"+sendtext+'&tmp='+Math.random();

$.get(url,function (data) {
    var stat=$(data).find("rsp").attr("stat");
    if(stat=="fail") {
        $(data).find('Error').each(function () {
            var code=$(this).attr('code');
            if(code==4) {
                alert(cannotModify);
            }else if(code==5) {
                gotologin();
            }
        });
    }else{
        flag = $(data).find("item").attr("value");
        if(flag=="0"){
            document.title="WROC";
            $("#td_logo").find("img").attr("src","images/login_logo.png");
            if(getCookie("Language")=="chinese"){
            languageType="chinese";
            $("#login_button").attr("src","images/login_button.png");
            $("#position").css({"margin-right":125});
            $("#product_image").attr("src","images/product.png");
            }else{
            languageType="english";
            $("#login_button").attr("src","images_en/login_button.png");
            $("#position").css({"margin-right":50});
            $("#product_image").attr("src","images_en/product.png");
            }
        }else{
            if(getCookie("Language")=="chinese"){
             languageType="chinese";
             $("#login_button").attr("src","images/login_button.png");

            $("#product_image").attr("src","ST/product.png");
            }else{
                $("#login_button").attr("src","images_en/login_button.png");
                languageType="english";

                $("#product_image").attr("src","ST/product_en.png");
            }
        }

    }
});

    $(document).ready(function () {
        document.onkeypress = function () {
            var iKeyCode = -1;
            if (arguments[0]) {
                iKeyCode = arguments[0].which;
            }
            else {
                iKeyCode = event.keyCode;
            }
            if (iKeyCode == 13) {
                loginWeb();
            }
        }
        $("label").each(function () {
            $(this).html(eval($(this).attr("sid")));
        });

        if(langauaCookie=="english"){
            languageType="english";
            $("#login_button").attr("src","images_en/login_button.png");

            $("#td_logo").find("img").attr("src","ST/login_logo.png");
            $("#product_image").attr("src","ST/product_en.png");
        }

        document.title=msg.webTitle;

        $("[type='button']").each(function () {
            $(this).val(eval($(this).attr("sid")));
        });
        $("#changeLanguage").click(function (e) {
            var $target = $(e.target);
            if ($target.attr("id") == "chooseEn") {
                languageType ="english";
                setCookie("Language", "english");
                $("#language").attr("src", "js/english.js");
                $("#login_button").attr("src","images_en/login_button.png");
                if(flag==0){
                    document.title="WROC";
                    $("#position").css({"margin-right":50});
                    $("#td_logo").find("img").attr("src","images/login_logo.png");
                    $("#product_image").attr("src","images_en/product.png");
                }else{
                    document.title="WiFi IPPBX";
                    $("#td_logo").find("img").attr("src","ST/login_logo.png");
                    $("#product_image").attr("src","ST/product_en.png");
                }
            } else {
                languageType ="chinese";
                setCookie("Language", "chinese");
                $("#language").attr("src", "js/chinese.js");
                $("#login_button").attr("src","images/login_button.png");
                if(flag==0){
                    document.title="WROC";
                    $("#position").css({"margin-right":125});
                    $("#td_logo").find("img").attr("src","images/login_logo.png");
                    $("#product_image").attr("src","images/product.png");
                }else{
                 document.title="无线IPPBX";
                 $("#td_logo").find("img").attr("src","ST/login_logo.png");
                 $("#product_image").attr("src","ST/product.png");
                }
            }
            window.location.reload();
        });

        var top = document.body.scrollHeight / 2 - 30;
        $("#login").css({
             "margin-top":getHeight()/3-30
         });

        $("form").submit(function () {
            return false;
        });
         $("#login_button").bind("mouseover",function(){
              var arr= $(this).attr("src").split("/");
              $(this).attr("src",arr[0]+"/login_button_hover.png");
         });
        $("#login_button").bind("mouseout",function(){
            var arr= $(this).attr("src").split("/");
            $(this).attr("src",arr[0]+"/login_button.png");
        });

        document.onkeypress = function () {
            var iKeyCode = -1;
            if (arguments[0]) {
                iKeyCode = arguments[0].which;
            }
            else {
                iKeyCode = event.keyCode;
            }
            if (iKeyCode == 13) {
                loginWeb();
            }
        }
    });


    var guideflag = 0;

    function loginWeb() {
        var user = $("[name=role]").val();
        var username = $.trim($("[name=username]").val());
        var password = $.trim($("[name=password]").val());
        if (username == "") {
            if(languageType=="chinese"){
               alert("请输入用户名。");
            }else{
                alert("Please input user name.");
            }
        } else if (username != "admin") {
            if(languageType=="chinese"){
                alert("用户名不正确，请重新输入。");
            }else{
                alert("Error. Please re-enter user name.");
            }
        } else {
            //if(user=="admin") {
            var settext = "&id51=" + password;
            //}else {
            //var settext="&id50="+password
            //}
            $.ajax({
                type:"POST", url:"xml?method=gw.account.login" + settext + "&tmp=" + Math.random(), timeout:5000, dataType:"xml", error:function (XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus == "timeout") {
                        window.location.reload();
                    }
                }, success:function (xml) {
                    $(xml).find('rsp').each(function () {
                        var stat = $(this).attr('stat');
                        if (stat == "ok") {
                            var admin = $(xml).find('item').attr('value');
                            if (admin == "admin") {
                                $.ajax({
                                    type:"POST", url:"xml?method=gw.config.get&id=2010&tmp=" + Math.random(), timeout:5000, dataType:"xml", error:function (XMLHttpRequest, textStatus, errorThrown) {
                                        if (textStatus == "timeout") {
                                            window.location.reload();
                                        }
                                    }, success:function (xml) {
                                        guideflag = $(xml).find("item").attr("value");
                                        var cookie = $(xml).find('item').attr('cookie');
                                        setCookie("admin", admin);
                                        setCookie("cookie", cookie);
                                        setCookie("language", "0");
                                        setCookie("model", "OM");
                                        if (guideflag == 0) {
                                            window.location.href = "menuGuide.htm?type=first&language="+languageType;
                                        } else {
                                            window.location.href = "menuGuide.htm?type=notFirst&language="+languageType;
                                        }
                                    }
                                });
                            } else if (admin == "view") {
                                var  confirmText="";
                              if(languageType=="chinese"){
                                  confirmText= "当前已有管理员与设备连接，是否确定继续连接？";
                              }else{
                                  confirmText= "There's been an adminstrator connected. Do you need to continue?";
                              }
                                if (confirm(confirmText)) {
                                    $.ajax({
                                        type:"POST", url:"xml?method=gw.account.login&user=admin&flag=force" + settext + "&tmp=" + Math.random(), timeout:5000, dataType:"xml", error:function (XMLHttpRequest, textStatus, errorThrown) {
                                            if (textStatus == "timeout") {
                                                window.location.reload();
                                            }
                                        }, success:function (xml) {
                                            $(xml).find('rsp').each(function () {
                                                $.ajax({
                                                    type:"POST", url:"xml?method=gw.config.get&id=2010&tmp=" + Math.random(), timeout:5000, dataType:"xml", error:function (XMLHttpRequest, textStatus, errorThrown) {
                                                        if (textStatus == "timeout") {
                                                            window.location.reload();
                                                        }
                                                    }, success:function (xml) {
                                                        guideflag = $(xml).find("item").attr("value");
                                                        var cookie = $(xml).find('item').attr('cookie');
                                                        setCookie("admin", "admin");
                                                        setCookie("cookie", cookie);
                                                        setCookie("language", "0");
                                                        setCookie("model", "OM");
                                                        if (guideflag == 0) {
                                                            window.location.href = "menuGuide.htm?type=first&language="+languageType;
                                                        } else {
                                                            window.location.href = "menuGuide.htm?type=notFirst&language="+languageType;
                                                        }
                                                    }
                                                });
                                            });
                                        }
                                    });
                                    return true;
                                } else {
                                    $.get("/xml?method=gw.account.logout&tmp=" + Math.random(), function (data) {
                                        if ($(data).find("rsp").attr("stat") == "ok") {
                                            deleteCookie("admin");
                                            deleteCookie("more");
                                            deleteCookie("cookie");
                                            window.location.href = "index.htm";
                                        }
                                    });
                                    return false;
                                }
                            }
                        } else if (stat == "fail") {
                            if(languageType=="chinese"){
                                alert("密码不正确，请重新输入。");
                            }else{
                                alert("Invalid password.");
                            }
                            window.location.href = "index.htm";
                        }
                    });
                }
            });
        }
    }
        function getHeight(){

            var yScroll;

            if (window.innerHeight && window.scrollMaxY) {
                yScroll = window.innerHeight + window.scrollMaxY;
            } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
                yScroll = document.body.scrollHeight;
            } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
                yScroll = document.body.offsetHeight;
            }

            var windowHeight;
            if (self.innerHeight) { // all except Explorer
                windowHeight = self.innerHeight;
            } else if (document.documentElement && document.documentElement.clientHeight) {
                // Explorer 6 Strict Mode
                windowHeight = document.documentElement.clientHeight;
            } else if (document.body) { // other Explorers
                windowHeight = document.body.clientHeight;
            }

            // for small pages with total height less then height of the viewport
            if(yScroll < windowHeight){
                pageHeight = windowHeight;
            } else {
                pageHeight = yScroll;
            }
            return pageHeight;
        }

</script>
</body>
</html>